/*
 * @Author: 肖思汗 
 * @Date: 2025-06-24 23:41:08 
 * @Last Modified by: xiaosihan
 * @Last Modified time: 2025-09-16 13:46:15
 */

import { autorun, makeAutoObservable } from "mobx";
import userApi from "src/pages/User/userApi";
import userInfoApi from "./userInfoApi";
import Taro from "@tarojs/taro";
import userStore from "src/pages/User/userStore";
import promoteRelationshipsApi from '../PromoteRelationships/promoteRelationshipsApi';
import { get } from 'lodash';

class UserInfoStore {
    constructor() {
        makeAutoObservable(this, {}, { deep: false });
        // autorun(() => {
        //     if (userStore.isLogin) {
        //         this.getCurrentUser();
        //         this.getOriginUserCode();
        //     }
        // });
    }

    id: number;
    userName: string; //昵称
    phoneNumber: string; //用户绑定的手机号（国外手机号会有区号）
    profilePicture: string; //头像
    sex: boolean; //性别 false为男 true为女
    userType: string; //用户类别
    birth: string; //生日
    promotionCode: string; //推广码图片
    userCode: string; //推广码
    promoCode: string = ""; //推广码
    originUserCode: string = ""; // 邀请人代码

    getCurrentUser() {
        return userInfoApi.getCurrentUser().then((res) => {
            this.userName = res.data.userName;
            this.phoneNumber = res.data.phoneNumber;
            this.profilePicture = res.data.profilePicture;
            this.sex = res.data.sex;
            this.userType = res.data.userType;
            this.birth = res.data.birth;
            this.promotionCode = res.data.promotionCode;
            this.userCode = res.data.userCode;
            this.id = res.data.id;
        });
    }



    updateUserInfo(data: {
        userName: string,//昵称
        profilePicture: string,//头像
        sex: boolean,
        birth: string,//生日
        phoneNumber: string,//手机号
    }) {
        return userInfoApi.updateUserInfo(data).then((res) => {
            if (res.code == 200) {
                Taro.showToast({
                    title: '保存成功',
                    icon: 'none'
                });
                this.getCurrentUser();
            }
            return res;
        })
    }

    // 授权手机号
    getPhoneNumber = async (e) => {
        console.log('获取手机号', e);
        // 这里处理获取手机号的逻辑
        if (e.detail.code) {
            // 通过code获取手机号
            console.log('获取手机号code:', e.detail.code);
            // 可以调用API获取手机号并更新store
            const res = await userInfoApi.getPhone(e.detail.code);
            // userInfoStore.phoneNumber = res.data.phoneNumber;
            // console.log('获取手机号结果:', res);
            await this.getCurrentUser();
        }
    };


    // 绑定邀请
    bindPromotionCode(promotionCode: string) {
        promoteRelationshipsApi.bindParentUserCode(promotionCode).then((res) => {
            if (res.code == 200) {
                let title = '绑定成功';
                if (res.msg.includes('券')) {
                    title = '领券并绑定成功';
                }
                Taro.showToast({
                    title: title,
                    icon: 'none',
                });
                this.getOriginUserCode();
            }
        })
    }

    // 获取邀请人代码
    getOriginUserCode() {
        promoteRelationshipsApi.getFirstParentInfo().then(res => {
            this.originUserCode = get(res, ["data", "userCode"], "");
        });
    }



}


const userInfoStore = new UserInfoStore();

export default userInfoStore;











